from CvPythonExtensions import *
# is this currently needed?
import CvUtil
import Popup as PyPopup
import PyHelpers

# globals
gc = CyGlobalContext()
PyPlayer = PyHelpers.PyPlayer

class CustomFunctions:

	def PlFSetWeapons(self, pUnit, pCity):
		iPlayer = pUnit.getOwner()
		pPlayer = gc.getPlayer(iPlayer)
		eTeam = gc.getTeam(pPlayer.getTeam())

		if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_KINETIC')):
			if eTeam.isHasTech(gc.getInfoTypeForString('TECH_MAGNETIC_ACCELERATION')):
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_MASS_DRIVER'), True)
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_KINETIC2'), False)
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_NANOMINIATURIZATION')):
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_KINETIC2'), True)
			if pCity.getNumBonuses(gc.getInfoTypeForString('BONUS_URANIUM')) > 0:
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_U-238_SHELLS'), True)
		if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_LASER')):
			if eTeam.isHasTech(gc.getInfoTypeForString('TECH_PHOTONICS')):
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_PHOTON_LASER'), True)
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_GATLING_LASER'), False)
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_PULSE_LASER'), False)
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_SUPERCONDUCTOR')):
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_GATLING_LASER'), True)
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_PULSE_LASER'), False)
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_HIGH_INTENSITY_LASERS')):
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_PULSE_LASER'), True)
		if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_MISSILE')):
			if eTeam.isHasTech(gc.getInfoTypeForString('TECH_FUSION_POWER')):
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_PLASMA_TORPEDO'), True)
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_MISSILE2'), False)
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_MISSILE1'), False)
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_ORBITAL_ENGINEERING')):
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_MISSILE2'), True)
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_MISSILE1'), False)
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_NUCLEAR_PHYSICS')):
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_MISSILE1'), True)

	def PlFUnitCostMod(self, iPlayer, iUnit):
		pPlayer = gc.getPlayer(iPlayer)
		eTeam = gc.getTeam(pPlayer.getTeam())

		iKineticInfantry = gc.getInfoTypeForString('UNIT_INFANTRY_KINETIC')
		iKineticRover = gc.getInfoTypeForString('UNIT_ROVER_KINETIC')
		iKineticHovertank = gc.getInfoTypeForString('UNIT_HOVERTANK_KINETIC')
		iKineticArtillery = gc.getInfoTypeForString('UNIT_ARTILLERY_KINETIC')
		iKineticFoil = gc.getInfoTypeForString('UNIT_FOIL_KINETIC')
		iKineticCruiser = gc.getInfoTypeForString('UNIT_CRUISER_KINETIC')
		iKineticSubmarine = gc.getInfoTypeForString('UNIT_SUBMARINE_KINETIC')
		iKineticHelicopter = gc.getInfoTypeForString('UNIT_HELICOPTER_KINETIC')
		iKineticNeedlejet = gc.getInfoTypeForString('UNIT_NEEDLEJET_KINETIC')
		iKineticGravship = gc.getInfoTypeForString('UNIT_GRAVSHIP_KINETIC')
		iLaserInfantry = gc.getInfoTypeForString('UNIT_INFANTRY_LASER')
		iLaserRover = gc.getInfoTypeForString('UNIT_ROVER_LASER')
		iLaserHovertank = gc.getInfoTypeForString('UNIT_HOVERTANK_LASER')
		iLaserArtillery = gc.getInfoTypeForString('UNIT_ARTILLERY_LASER')
		iLaserFoil = gc.getInfoTypeForString('UNIT_FOIL_LASER')
		iLaserCruiser = gc.getInfoTypeForString('UNIT_CRUISER_LASER')
#		iLaserSubmarine = gc.getInfoTypeForString('UNIT_SUBMARINE_LASER')
		iLaserHelicopter = gc.getInfoTypeForString('UNIT_HELICOPTER_LASER')
		iLaserNeedlejet = gc.getInfoTypeForString('UNIT_NEEDLEJET_LASER')
		iLaserGravship = gc.getInfoTypeForString('UNIT_GRAVSHIP_LASER')
		iMissileInfantry = gc.getInfoTypeForString('UNIT_INFANTRY_MISSILE')
		iMissileRover = gc.getInfoTypeForString('UNIT_ROVER_MISSILE')
		iMissileHovertank = gc.getInfoTypeForString('UNIT_HOVERTANK_MISSILE')
		iMissileArtillery = gc.getInfoTypeForString('UNIT_ARTILLERY_MISSILE')
		iMissileFoil = gc.getInfoTypeForString('UNIT_FOIL_MISSILE')
		iMissileCruiser = gc.getInfoTypeForString('UNIT_CRUISER_MISSILE')
		iMissileSubmarine = gc.getInfoTypeForString('UNIT_SUBMARINE_MISSILE')
		iMissileHelicopter = gc.getInfoTypeForString('UNIT_HELICOPTER_MISSILE')
		iMissileNeedlejet = gc.getInfoTypeForString('UNIT_NEEDLEJET_MISSILE')
		iMissileGravship = gc.getInfoTypeForString('UNIT_GRAVSHIP_MISSILE')

		KineticList = [iKineticInfantry, iKineticRover, iKineticHovertank, iKineticArtillery, iKineticFoil, iKineticCruiser, iKineticSubmarine, iKineticHelicopter, iKineticNeedlejet, iKineticGravship]
		LaserList = [iLaserInfantry, iLaserRover, iLaserHovertank, iLaserArtillery, iLaserFoil, iLaserCruiser, iLaserHelicopter, iLaserNeedlejet, iLaserGravship]
		MissileList = [iMissileInfantry, iMissileRover, iMissileHovertank, iMissileArtillery, iMissileFoil, iMissileCruiser, iMissileSubmarine, iMissileHelicopter, iMissileNeedlejet, iMissileGravship]

		if (iUnit in KineticList):
			if eTeam.isHasTech(gc.getInfoTypeForString('TECH_MAGNETIC_ACCELERATION')):
				return 350
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_NANOMINIATURIZATION')):
				return 250
		if (iUnit in LaserList):
			if eTeam.isHasTech(gc.getInfoTypeForString('TECH_PHOTONICS')):
				return 350
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_SUPERCONDUCTOR')):
				return 250
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_HIGH_INTENSITY_LASERS')):
				return 150
		if (iUnit in MissileList):
			if eTeam.isHasTech(gc.getInfoTypeForString('TECH_FUSION_POWER')):
				return 350
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_ORBITAL_ENGINEERING')):
				return 250
			elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_NUCLEAR_PHYSICS')):
				return 150

		return -1

	def PFFindClearPlot(self, pUnit, plot):
		BestPlot = -1
		iBestPlot = 0
		if pUnit == -1:
			iX = plot.getX()
			iY = plot.getY()
			for iiX in range(iX-1, iX+2, 1):
				for iiY in range(iY-1, iY+2, 1):
					iCurrentPlot = 0
					pPlot = CyMap().plot(iiX,iiY)
					if pPlot.getNumUnits() == 0:
						if (pPlot.isWater() == plot.isWater() and pPlot.isCity() == False):
							iCurrentPlot = iCurrentPlot + 5
					if iCurrentPlot >= 1:
						iCurrentPlot = iCurrentPlot + CyGame().getSorenRandNum(5, "PFFindClearPlot")
						if iCurrentPlot >= iBestPlot:
							BestPlot = pPlot
							iBestPlot = iCurrentPlot
			return BestPlot
		iX = pUnit.getX()
		iY = pUnit.getY()
		for iiX in range(iX-1, iX+2, 1):
			for iiY in range(iY-1, iY+2, 1):
				iCurrentPlot = 0
				pPlot = CyMap().plot(iiX,iiY)
				if pPlot.getNumUnits() == 0:
					if pUnit.canMoveOrAttackInto(pPlot, False):
						iCurrentPlot = iCurrentPlot + 5
				for i in range(pPlot.getNumUnits()):
					if pPlot.getUnit(i).getOwner() == pUnit.getOwner():
						if pUnit.canMoveOrAttackInto(pPlot, False):
							iCurrentPlot = iCurrentPlot + 15
				if pPlot.isCity():
					if pPlot.getPlotCity().getOwner() == pUnit.getOwner():
						iCurrentPlot = iCurrentPlot + 50
				if (iX == iiX and iY == iiY):
					iCurrentPlot = 0
				if iCurrentPlot >= 1:
					iCurrentPlot = iCurrentPlot + CyGame().getSorenRandNum(5, "PFFindClearPlot")
					if iCurrentPlot >= iBestPlot:
						BestPlot = pPlot
						iBestPlot = iCurrentPlot
		return BestPlot

	def PFFindClearPlotForMindWorm(self, pUnit, plot):
		BestPlot = -1
		iBestPlot = 0
		if pUnit == -1:
			iX = plot.getX()
			iY = plot.getY()
			for iiX in range(iX-1, iX+2, 1):
				for iiY in range(iY-1, iY+2, 1):
					iCurrentPlot = 0
					pPlot = CyMap().plot(iiX,iiY)
					if pPlot.getNumUnits() == 0:
						if (pPlot.isWater() == plot.isWater() and pPlot.isCity() == False):
							iCurrentPlot = iCurrentPlot + 5
						if pPlot.getFeatureType() == gc.getInfoTypeForString('FEATURE_XENOFUNGUS'):
							iCurrentPlot = iCurrentPlot + 5
					for i in range(pPlot.getNumUnits()):
						if pPlot.getUnit(i).getOwner() == gc.getPlayer(gc.getBARBARIAN_PLAYER()):
							if (pPlot.isWater() == plot.isWater() and pPlot.isCity() == False):
								iCurrentPlot = iCurrentPlot + 5
							if pPlot.getFeatureType() == gc.getInfoTypeForString('FEATURE_XENOFUNGUS'):
								iCurrentPlot = iCurrentPlot + 5
					if iCurrentPlot >= 1:
						iCurrentPlot = iCurrentPlot + CyGame().getSorenRandNum(5, "PFFindClearPlot")
						if iCurrentPlot >= iBestPlot:
							BestPlot = pPlot
							iBestPlot = iCurrentPlot
			return BestPlot
		iX = pUnit.getX()
		iY = pUnit.getY()
		for iiX in range(iX-1, iX+2, 1):
			for iiY in range(iY-1, iY+2, 1):
				iCurrentPlot = 0
				pPlot = CyMap().plot(iiX,iiY)
				if pPlot.getNumUnits() == 0:
					if pUnit.canMoveOrAttackInto(pPlot, False):
						iCurrentPlot = iCurrentPlot + 5
					if pPlot.getFeatureType() == gc.getInfoTypeForString('FEATURE_XENOFUNGUS'):
						iCurrentPlot = iCurrentPlot + 5
				for i in range(pPlot.getNumUnits()):
					if pPlot.getUnit(i).getOwner() == pUnit.getOwner():
						if pUnit.canMoveOrAttackInto(pPlot, False):
							iCurrentPlot = iCurrentPlot + 15
				if pPlot.isCity():
					if pPlot.getPlotCity().getOwner() == pUnit.getOwner():
						iCurrentPlot = iCurrentPlot + 50
				if (iX == iiX and iY == iiY):
					iCurrentPlot = 0
				if iCurrentPlot >= 1:
					iCurrentPlot = iCurrentPlot + CyGame().getSorenRandNum(5, "PFFindClearPlot")
					if iCurrentPlot >= iBestPlot:
						BestPlot = pPlot
						iBestPlot = iCurrentPlot
		return BestPlot

